﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Movie.DAL
{
   public  class MovieDAL
   {
        //使用集合获取表中所有列中的数据
        public static List<Modal.Movies> GetMovies()
        {
            string sql = "select top 10 * from Movie "; //order by MreleaseDate Desc
           
            SqlDataReader reder =sqlHelper2.ExcuteReader(sql);
            //读取全部数据
            List<Modal.Movies> list = new List<Modal.Movies>();
            //读取的是第一条数据
            while (reder.Read())
            {
                Modal.Movies movie = new Modal.Movies();
                movie.MovieId = Convert.ToInt32(reder["MovieId"]);
                movie.Mname = reder["Mname"].ToString();
                movie.Mprice = Convert.ToInt32(reder["Mprice"]);          
                movie.Mpicture = reder["Mpicture"].ToString();
                movie.Mbrief = reder["Mbrief"].ToString();
                movie.Mdirector = reder["Mdirector"].ToString();
                movie.Mactor = reder["Mactor"].ToString();
                movie.Moutdate = Convert.ToDateTime(reder["Moutdate"]);
                movie.MreleaseDate = Convert.ToDateTime(reder["MreleaseDate"]);
                movie.Mscore =float.Parse( reder["Mscore"].ToString());
                movie.Moutdate = Convert.ToDateTime(reder["Moutdate"]);
                list.Add(movie);
            }

            reder.Close();
            return list;
        }
       //详情信息
        public static List<Modal.Movies> GetDetail(string id)
        {
            string sql =string .Format( "select * from Movie where MovieId={0}",id);
            List<Modal.Movies> list = new List<Modal.Movies>();
            SqlDataReader reder = sqlHelper2.ExcuteReader(sql);
            Modal.Movies movie = null;
           if(reder.Read())
            {
              movie = new Modal.Movies();
                movie.MovieId = Convert.ToInt32(reder["MovieId"]);
                movie.Mname = reder["Mname"].ToString();
                movie.Mprice = Convert.ToInt32(reder["Mprice"]);
                movie.Mpicture = reder["Mpicture"].ToString();
                movie.Mbrief = reder["Mbrief"].ToString();
                movie.Mdirector = reder["Mdirector"].ToString();
                movie.Mactor = reder["Mactor"].ToString();
                movie.Moutdate = Convert.ToDateTime(reder["Moutdate"]);
                movie.MreleaseDate = Convert.ToDateTime(reder["MreleaseDate"]);
                movie.Mscore = float.Parse(reder["Mscore"].ToString());
                movie.Moutdate = Convert.ToDateTime(reder["Moutdate"]);
                list.Add(movie);
            }
            reder.Close();
            return list;
        }

       public static Modal.Movies GetMovieByScheduleId(int scheduleId) 
       {
           string sql = string.Format("select * from Movie where movieid=(select movieid from schedule where scheduleid={0})", scheduleId);
           SqlDataReader reder = sqlHelper2.ExcuteReader(sql);
           Modal.Movies movie = null;
           if (reder.Read())
           {
               movie = new Modal.Movies();
                movie.MovieId = Convert.ToInt32(reder["MovieId"]);
                movie.Mname = reder["Mname"].ToString();
                movie.Mprice = Convert.ToInt32(reder["Mprice"]);          
                movie.Mpicture = reder["Mpicture"].ToString();
                movie.Mbrief = reder["Mbrief"].ToString();
                movie.Mdirector = reder["Mdirector"].ToString();
                movie.Mactor = reder["Mactor"].ToString();
                movie.Moutdate = Convert.ToDateTime(reder["Moutdate"]);
                movie.MreleaseDate = Convert.ToDateTime(reder["MreleaseDate"]);
                movie.Mscore =float.Parse( reder["Mscore"].ToString());
                movie.Moutdate = Convert.ToDateTime(reder["Moutdate"]);  
           }
           return movie;

       }


       public static List<Modal.MovieType> GetMovieType()
       { 
           string sql ="select * from MovieType ";
           SqlDataReader reder = sqlHelper2.ExcuteReader(sql);
           List<Modal.MovieType> list = new List<Modal.MovieType>();
           Modal.MovieType type = null;
           while (reder.Read())
           {
               type = new Modal.MovieType();
               type.TypeID = Convert.ToInt32(reder["TypeID"]);
               type.TypeName = reder["TypeName"].ToString();
               list.Add(type);
           }
           return list;
       }
    }
}
